<?php
	class Model_date extends CI_Model{

		function count_weeknum_of_year(){
			$last_week_of_year_temst=mktime(0,0,0,12,31,2012);
			return $last_week_of_year=date('W',$last_week_of_year_temst-7*24*24);
		}

		public function get_weeknum_of_year_array(){
			return $this->weeknum_array();
		}

		private function weeknum_array(){
			$last_week_of_year=$this->count_weeknum_of_year(2012);
			for ($weeknum=1; $weeknum <=$last_week_of_year; $weeknum++) { 
				$weeknum_array[]=$weeknum;
			}
			return $weeknum_array;
		}

		function get_weeknum_of_today(){
			return date('W');
		}

		function get_weeknum_by_date($date){
			$date=explode("-", $date);
			$date_timstampt=mktime(0,0,0,$date[1],$date[2],$date[0]);
			return date('W',$date_timstampt);
		}

		function get_week_date($year,$weeknum){
		    $firstdayofyear=mktime(0,0,0,1,1,$year);
		    $firstweekday=date('N',$firstdayofyear);
		    $firstweenum=date('W',$firstdayofyear);
		    if($firstweenum==1){
		        $day=(1-($firstweekday-1))+7*($weeknum-1);
		        $startdate=date('Y-m-d',mktime(0,0,0,1,$day,$year));
		        $enddate=date('Y-m-d',mktime(0,0,0,1,$day+6,$year));
		    }else{
		        $day=(9-$firstweekday)+7*($weeknum-1);
		        $startdate=date('Y-m-d',mktime(0,0,0,1,$day,$year));
		        $enddate=date('Y-m-d',mktime(0,0,0,1,$day+6,$year));
		    }
    	return array($startdate,$enddate);    
		}

	}
?>